package com.qfedu.department.tasklistener;

import java.util.List;

import org.activiti.engine.HistoryService;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;

import com.qfedu.common.util.WoUtil;
import com.qfedu.department.service.StaffService;

public class DepartmentReviewTaskListener implements TaskListener {

	@Override
	public void notify(DelegateTask delegateTask) {
		// 获取流程实例id
		String id = delegateTask.getProcessInstanceId();
		// 获取Activiti历史服务
		HistoryService his = WoUtil.getContext().getBean(HistoryService.class);
		// 查询申请人id
		String staffId = his.createHistoricVariableInstanceQuery().processInstanceId(id).variableName("applyId")
				.singleResult().getValue().toString();
		// 通过申请人的id，查询其所在部门的部门主管
		StaffService staffService = WoUtil.getContext().getBean(StaffService.class);
		List<String> managers = staffService.getManagers(staffId);
		// 将部门主管的id加入该任务的候选人中
		delegateTask.addCandidateUsers(managers);
	}

}
